package com.oppo.skywalking.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oppo.skywalking.entity.Employee;
import com.oppo.skywalking.result.BusinessResult;
import com.oppo.skywalking.service.EmployeeService;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 雇员表(employee)表控制层
 * @author makejava
 * @since 2022-11-06 13:16:32
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    /**
     * 分页查询所有数据
     * @param page     分页对象
     * @param employee 查询实体
     * @return 所有数据
     */
    @Trace
    @Tag(key = "selectAll", value = "returnObj")
    @GetMapping(value = "/selectAll")
    public BusinessResult selectAll(Page<Employee> page, Employee employee) {
        return BusinessResult.success(this.employeeService.page(page, new QueryWrapper<>(employee)));
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "/{id}")
    public BusinessResult<Employee> selectOne(@PathVariable Serializable id) {
        return BusinessResult.success(this.employeeService.getById(id));
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键id
     * @return 员工信息
     */
    @GetMapping(value = "/find/{id}")
    public BusinessResult<Employee> findById(@PathVariable Long id) {
        return BusinessResult.success(this.employeeService.findById(id));
    }

    /**
     * 新增数据
     * @param employee 实体对象
     * @return 新增结果
     */
    @PostMapping(value = "/insert")
    public BusinessResult<Boolean> insert(@RequestBody Employee employee) {
        return BusinessResult.success(this.employeeService.save(employee));
    }

    /**
     * 修改数据
     * @param employee 实体对象
     * @return 修改结果
     */
    @PutMapping
    public BusinessResult<Boolean> update(@RequestBody Employee employee) {
        return BusinessResult.success(this.employeeService.updateById(employee));
    }

    /**
     * 删除数据
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public BusinessResult<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        return BusinessResult.success(this.employeeService.removeByIds(idList));
    }
}
